$(function () {

    var edit_dialog =  $('#edit_dialog');
    var edit_form =  $('#edit_form');
    var all_datagrid =  $('#all_datagrid');

    //数据表格
    all_datagrid.datagrid({

        url:'/curriculum/list.do',
        striped:true,
        singleSelect:true,
        pagination:true,
        //自适应表格
        fitColumns:true,
        rownumbers:true,
        toolbar:"#tb",
        columns:[[
            {field:'cb',checkbox:true},
            {field:'id',hidden:true,width:100},
            {field:'clazz',title:'班级',width:100,formatter:function (value,row,index) {

                return value?value.name:""
                }},
            {field:'weekday',title:'星期',width:100},
            {field:'subject',title:'科目',width:100,formatter:function (value,row,index) {
                return value?value.name:"";
                }},
            {field:'teacher',title:'上课教师',width:100,formatter:function (value,row,index) {
                    return value?value.username:"";
                }},
            {field:'classroom',title:'教室',width:100,formatter:function (value,row,index) {

                return value?value.name:"";
                }},
            {field:'schooltime',title:'上课时间',width:100},
            {field:'remark',title:'备注',width:100},
            {field:'state',title:'状态',width:100,formatter:function (value,row,index) {
                    if (value==1){

                        return "<span style='color: #4cae4c'>已上</span>";
                    }

                    if (value ==0){

                        return "<span style='color:red'>未上</span>";
                    }

                }}
        ]]


 
    });

    //end


    var cmdObj = {

        //添加按钮添加课程表
        add : function () {

            edit_dialog.dialog("open");
            edit_dialog.dialog("setTitle","添加课程表");
            edit_form.form("clear");

        },
        //编辑按钮编辑课程表
        edit : function () {

            var row = all_datagrid.datagrid("getSelected");
            if (!row){
                $.messager.alert("温馨提示","请选择要编辑的课程表","info");
                return;
            }

            edit_dialog.dialog("setTitle","编辑课程表");
            //老师回显
            if (row.course){

                row["course.id"] = row.course.id;
            }
            //班级回显
            if (row.clazz){

                row["clazz.id"] = row.clazz.id;
            }
            //科目回显
            if (row.subject){

                row["subject.id"] = row.subject.id;
            }
            //上课教师回显
            if (row.teacher){

                row["teacher.id"] = row.teacher.id;
            }

            //教室
            if (row.classroom){

                row["classroom.id"] = row.classroom.id;
            }




            //在读取数据之前,清除缓存
            edit_form.form("clear");

            edit_form.form("load",row);

            edit_dialog.dialog("open");

        },

        //save按钮提交表单
        save : function () {

            var url = "";

            var id = $("#deptId").val();

            if(id){
                url = "/curriculum/update.do";
            }else{
                url = "/curriculum/save.do";
            }


            edit_form.form('submit', {
                url:url,
                success: function(data){
                    var data = eval('(' + data + ')');
                    if (data.success){
                        $.messager.confirm('温馨提示','操作成功',function(r){
                            edit_dialog.dialog("close");
                            all_datagrid.datagrid("load");
                        });
                    }else{
                        $.messager.alert("温馨提示",data.msg);
                    }
                }
            });
        },
        //编辑的关闭按钮功能
        cancel : function () {
            edit_dialog.dialog("close");
        },
        //删除课程表
        remove : function () {
            var row = all_datagrid.datagrid("getSelected");
            if (!row){
                $.messager.alert("温馨提示","请选择要删除的课程表","info");
                return;
            }

            $.messager.confirm('确认','您确认想要删除记录吗？',function(r){
                if (r){
                    $.get("/curriculum/delete.do?id=" + row.id,function (data) {
                        if (data.success){
                            $.messager.confirm('温馨提示','删除成功',function(r){
                                all_datagrid.datagrid("load");
                            });
                        }else{
                            $.messager.alert("温馨提示",data.msg,"error");
                        }
                    })
                }
            });
        },
        //刷新
        reload :function () {
            all_datagrid.datagrid("reload");
        },
        //高级查询
        searchT:function () {
        var kw = $("#keyword").val();
        //加载列表式,携带额外的参数
        $("#all_datagrid").datagrid("load",{
            keyword:kw

        })
    }

    }

    //抽取优化
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");

        cmdObj[cmd]();
    })
    //end


    //dialog弹窗
    $('#edit_dialog').dialog({
        title: 'My Dialog',
        buttons:"#bb",
        closed:true,
        width: 350,
        height: 400,
    });
    //end

})//end















